package com.rickpan.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.rickpan.dto.request.GenerateReportRequest;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 生成报告响应DTO
 */
public class GeneratedReportResponse {

    private GenerateReportRequest.ReportType type;
    private String typeDescription;

    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate startDate;

    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate endDate;

    private String content;
    private Integer recordCount;
    private Integer totalTimeSpent; // 分钟
    private String formattedTimeSpent; // 格式化的时间显示

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime generatedAt;

    // 构造函数
    public GeneratedReportResponse() {
        this.generatedAt = LocalDateTime.now();
    }

    public GeneratedReportResponse(GenerateReportRequest.ReportType type, LocalDate startDate, LocalDate endDate, String content) {
        this();
        this.type = type;
        this.typeDescription = type.getDescription();
        this.startDate = startDate;
        this.endDate = endDate;
        this.content = content;
    }

    // Getter 和 Setter 方法
    public GenerateReportRequest.ReportType getType() {
        return type;
    }

    public void setType(GenerateReportRequest.ReportType type) {
        this.type = type;
        this.typeDescription = type != null ? type.getDescription() : null;
    }

    public String getTypeDescription() {
        return typeDescription;
    }

    public void setTypeDescription(String typeDescription) {
        this.typeDescription = typeDescription;
    }

    public LocalDate getStartDate() {
        return startDate;
    }

    public void setStartDate(LocalDate startDate) {
        this.startDate = startDate;
    }

    public LocalDate getEndDate() {
        return endDate;
    }

    public void setEndDate(LocalDate endDate) {
        this.endDate = endDate;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Integer getRecordCount() {
        return recordCount;
    }

    public void setRecordCount(Integer recordCount) {
        this.recordCount = recordCount;
    }

    public Integer getTotalTimeSpent() {
        return totalTimeSpent;
    }

    public void setTotalTimeSpent(Integer totalTimeSpent) {
        this.totalTimeSpent = totalTimeSpent;
        // 自动格式化时间显示
        if (totalTimeSpent != null) {
            int hours = totalTimeSpent / 60;
            int minutes = totalTimeSpent % 60;
            if (hours > 0) {
                this.formattedTimeSpent = hours + "小时" + (minutes > 0 ? minutes + "分钟" : "");
            } else {
                this.formattedTimeSpent = minutes + "分钟";
            }
        }
    }

    public String getFormattedTimeSpent() {
        return formattedTimeSpent;
    }

    public void setFormattedTimeSpent(String formattedTimeSpent) {
        this.formattedTimeSpent = formattedTimeSpent;
    }

    public LocalDateTime getGeneratedAt() {
        return generatedAt;
    }

    public void setGeneratedAt(LocalDateTime generatedAt) {
        this.generatedAt = generatedAt;
    }

    /**
     * 获取报告标题
     */
    public String getTitle() {
        if (type == null) return "工作报告";
        
        switch (type) {
            case DAILY:
                return startDate != null ? startDate + " 工作日报" : "工作日报";
            case WEEKLY:
                return (startDate != null && endDate != null) ? 
                       startDate + " 至 " + endDate + " 工作周报" : "工作周报";
            case MONTHLY:
                return (startDate != null && endDate != null) ? 
                       startDate + " 至 " + endDate + " 工作月报" : "工作月报";
            default:
                return "工作报告";
        }
    }

    @Override
    public String toString() {
        return "GeneratedReportResponse{" +
                "type=" + type +
                ", startDate=" + startDate +
                ", endDate=" + endDate +
                ", recordCount=" + recordCount +
                ", totalTimeSpent=" + totalTimeSpent +
                ", generatedAt=" + generatedAt +
                '}';
    }
}
